package com.jackstraw.jack_base.datasource.mybatis.base.entity.po;


import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.function.ReflectionUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Data
public abstract class PoExample<T, PO extends MyBatisPo<PO>> {

    /**
     * 实体类Class
     */
    protected Class<PO> poClass = currentPOClass();
    /**
     * PoExample Class
     */
    protected Class<T> poExampleClass = currentPOExampleClass();

    /**
     * 排序字段
     */
    protected String orderByClause;

    /**
     * 是否去重
     */
    protected boolean distinct;

    /**
     * 每页大小
     */
    protected Integer pageSize;

    /**
     * 查询页面
     */
    protected Integer pageNum = 0;

    /**
     * 查询起始索引
     */
    protected Integer index;

    protected List<T> oredCriteria;

    public PoExample() {
        oredCriteria = new ArrayList<>();
    }

    public T or() {
        T criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    public T createCriteria() {
        T criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    protected abstract T createCriteriaInternal();

    private Class<PO> currentPOClass() {
        return ReflectionUtil.getClassGenricType(this.getClass(), 1);
    }

    private Class<T> currentPOExampleClass() {
        return ReflectionUtil.getClassGenricType(this.getClass(), 0);
    }

    public abstract void addKey(Map<String, Object> params);

    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
        pageNum = null;
        pageSize = null;
    }

    public Integer getIndex() {
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(this.index)) {
            if (ObjectUtil.ObjectUtilByLang3.isNotEmpty(this.pageSize)
                    && ObjectUtil.ObjectUtilByLang3.isNotEmpty(this.pageNum)) {
                return this.pageSize * this.pageNum;
            }
            return 0;
        }
        return index;
    }

    public static class Criterion {
        private String condition;

        private Object value;

        private Object secondValue;

        private boolean noValue;

        private boolean singleValue;

        private boolean betweenValue;

        private boolean listValue;

        private String typeHandler;

        public String getCondition() {
            return condition;
        }

        public Object getValue() {
            return value;
        }

        public Object getSecondValue() {
            return secondValue;
        }

        public boolean isNoValue() {
            return noValue;
        }

        public boolean isSingleValue() {
            return singleValue;
        }

        public boolean isBetweenValue() {
            return betweenValue;
        }

        public boolean isListValue() {
            return listValue;
        }

        public String getTypeHandler() {
            return typeHandler;
        }

        public Criterion(String condition) {
            super();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
        }

        public Criterion(String condition, Object value, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
        }

        public Criterion(String condition, Object value) {
            this(condition, value, null);
        }

        public Criterion(String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
        }

        public Criterion(String condition, Object value, Object secondValue) {
            this(condition, value, secondValue, null);
        }
    }
}
